function [nsf, err] = read_control(command, filename)
%READ_CONTROL Read exported control worksheets from Origin projects
% 9/30/98 PBManis
%
global CONTROL
global ctl_k
%nargin
ctltab=[];
nsf=0;
err = 0;
%filename
if(~exist('ctl_k', 'var'))
    ctl_k = 0;
end;
switch(command)
    case 'import'
        clear ctltab;
        if(isempty(filename))
            err=1;
        end
        watchon;
        [ctl, line, err] = readctlfile(filename);
        if(err ~= 0)
            watchoff;
            return
        end
        nsf = size(line)-1;
        for i=1:nsf(2)
            remainder = line(i).data;
            all_arg = '';
            %   delimiters = '	';
            while(any(remainder))
                [chopped, remainder] = strtok(remainder);
                all_arg = strvcat(all_arg, chopped);
            end
            ctl_k = 0;
            ctltab(i).seqname=deblank(all_arg(next,:));
            ctltab(i).filename = deblank(all_arg(next,:));
            ctltab(i).path = deblank(all_arg(next,:));
            %ctltab(i).ctl
            a = deblank(all_arg(next,:));
            %ctltab(i).done =
            a = deblank(all_arg(next,:));
            ctltab(i).quality = deblank(all_arg(next,:));
            ctltab(i).mode = deblank(all_arg(next,:));
            ctltab(i).protocol = deblank(all_arg(next,:));
            ctltab(i).solution = deblank(all_arg(next,:));
            ctltab(i).pnd = 0;
            ctltab(i).div = 0;
            ctltab(i).sequence = deblank(all_arg(next,:));
            %ctltab(i).id =
            a = deblank(all_arg(next,:));
            ctltab(i).recbeg = str2num(deblank(all_arg(next,:)));
            ctltab(i).recend = str2num(deblank(all_arg(next,:)));
            ctltab(i).skip = str2num(deblank(all_arg(next,:)));
            ctltab(i).groupavg = str2num(deblank(all_arg(next,:)));
            ctltab(i).vgain = str2num(deblank(all_arg(next,:)));
            ctltab(i).igain = str2num(deblank(all_arg(next,:)));
            ctltab(i).wgain = str2num(deblank(all_arg(next,:)));
            ctltab(i).tbeg = str2num(deblank(all_arg(next,:)));
            ctltab(i).tend = str2num(deblank(all_arg(next,:)));
            ctltab(i).thresh = str2num(deblank(all_arg(next,:)));
            ctltab(i).jpot = number_arg(deblank(all_arg(next,:))); if(isempty(ctltab(i).jpot)) ctltab(i).jpot = 0; end;
            ctltab(i).access = str2num(deblank(all_arg(next,:))); if(isempty(ctltab(i).access)) ctltab(i).access = 0; end;
            ctltab(i).comp = str2num(deblank(all_arg(next,:)));
            ctltab(i).leakwin = str2num(deblank(all_arg(next,:)));
            ctltab(i).alignwin = str2num(deblank(all_arg(next,:)));
            ctltab(i).transwin = str2num(deblank(all_arg(next,:)));
            ctltab(i).transwdur = str2num(deblank(all_arg(next,:)));
            ctltab(i).deadwin = str2num(deblank(all_arg(next,:)));
            % ctltab(i).autosw
            a = deblank(all_arg(next,:));
            ctltab(i).durho = str2num(deblank(all_arg(next,:)));
            ctltab(i).durs1 = str2num(deblank(all_arg(next,:)));
            ctltab(i).durs2 = str2num(deblank(all_arg(next,:)));
            ctltab(i).durs3 = str2num(deblank(all_arg(next,:)));
            ctltab(i).diff = str2num(deblank(all_arg(next,:)));
            ctltab(i).file = deblank(all_arg(next,:));
            ctltab(i).altmode = str2num(deblank(all_arg(next,:)));
            ctltab(i).bactn = str2num(deblank(all_arg(next,:)));
            ctltab(i).cycle = str2num(deblank(all_arg(next,:)));
            ctltab(i).bstart = str2num(deblank(all_arg(next,:)));
            ctltab(i).bend = str2num(deblank(all_arg(next,:)));
            ctltab(i).vlmax = str2num(deblank(all_arg(next,:)));
            ctltab(i).vlmin = str2num(deblank(all_arg(next,:)));
            ctltab(i).leakt = str2num(deblank(all_arg(next,:)));
            ctltab(i).Gin = str2num(deblank(all_arg(next,:)));
            ctltab(i).Rin = str2num(deblank(all_arg(next,:)));
            ctltab(i).iHold = str2num(deblank(all_arg(next,:)));
            ctltab(i).Vm0 = str2num(deblank(all_arg(next,:)));
            ctltab(i).Rmp = 0;
            % ctltab(i).struct =
            a = deblank(all_arg(next,:));
            % ctltab(i).fileinfo =
            a = deblank(all_arg(next,:));
            % ctltab(i).entrydate
            a = deblank(all_arg(next,:));
            t1 = deblank(all_arg(next,:));
            if(~isempty(a) && ~isempty(t1)) ctltab(i).entrydate = strcat(a, t1); end
            % ctltab(i).lastchanged
            a = deblank(all_arg(next,:));
            t1 = deblank(all_arg(next,:));
            if(~isempty(a) && ~isempty(t1))  ctltab(i).lastchange = strcat(a, t1); end;
            % ctltab(i).linktype
            ty = str2num(deblank(all_arg(next,:)));
            if(~isempty(ty) && ty == 2)
                ctltab(i).subtract = 'sub';
            else
                ctltab(i).subtract = 'no';
            end
            if(~isempty(ty) && ty == 2)
                ctltab(i).linkfile = deblank(all_arg(next,:));
            else
                ctltab(i).linkfile = '';
            end
            ctltab(i).sub_frec = 1;
            ctltab(i).sub_lrec = 1;
            ctltab(i).sub_access = 0;
            ctltab(i).sub_comp = 0;
            ctltab(i).sub_igain = 1;

            %ctltab(i).linkparams = deblank(all_arg(next,:));
            %     ctltab(i).linkP2 = deblank(all_arg(next,:));
            %     ctltab(i).linkP3 = deblank(all_arg(next,:));
            %    ctltab(i).stimdel = deblank(all_arg(next,:));
            %   ctltab(i).stimipi = deblank(all_arg(next,:));

            %      disp(sprintf('Processing: %s %s %s', ctltab(i).filename, ctltab(i).path, ctltab(i).protocol))
            CONTROL = ctltab;
            %      setuserdata(ctltab);
            watchoff;
        end

    case 'import_from_summary'
        clear ctltab;
        if(isempty(filename))
            err=1;
        end
        watchon;
        load(filename);
        if(err ~= 0)
            watchoff;
            return
        end
        nsf = length(summary);
        i = 0;
        for k=1:nsf
            QueMessage(sprintf('%s, nproto: %d', summary(k).filename, length(summary(k).protocols)));
            for j=1:length(summary(k).protocols)
                i = i+1; % main index to update
                ctltab(i).seqname=' ';
                ctltab(i).filename = summary(k).filename;
                ctltab(i).path = summary(k).path;
                ctltab(i).quality = '';
                ctltab(i).mode = 'VC';
                ctltab(i).protocol = summary(k).protocols(j).proto;
                ctltab(i).solution = '';
                ctltab(i).pnd = 0;
                ctltab(i).div = 0;
                ctltab(i).sequence = summary(k).protocols(j).seq;
                ctltab(i).recbeg = summary(k).protocols(j).frec;
                ctltab(i).recend = summary(k).protocols(j).lrec;
                ctltab(i).seqname = make_sequence(summary(i).filename, summary(k).protocols(j).frec); % update sequence name now
                ctltab(i).skip = 1;
                ctltab(i).groupavg = 1;
                ctltab(i).vgain = 10;
                ctltab(i).igain = 1;
                ctltab(i).wgain = 1;
                ctltab(i).tbeg = 0;
                ctltab(i).tend = 200;
                ctltab(i).thresh = 0;
                ctltab(i).jpot = 0;
                ctltab(i).access = 0;
                ctltab(i).comp = 0;
                ctltab(i).leakwin = 2;
                ctltab(i).alignwin = 0;
                ctltab(i).transwin = 2;
                ctltab(i).transwdur = 45;
                ctltab(i).deadwin = 0;
                ctltab(i).durho = 5;
                ctltab(i).durs1 = 100;
                ctltab(i).durs2 = 0;
                ctltab(i).durs3 = 0;
                ctltab(i).diff = 0;
                ctltab(i).file = 0;
                ctltab(i).altmode = 0;
                ctltab(i).bactn = 1;
                ctltab(i).cycle = 1000;
                ctltab(i).bstart = 0;
                ctltab(i).bend = 2;
                ctltab(i).vlmax = -120;
                ctltab(i).vlmin = -60;
                ctltab(i).leakt = 11;
                ctltab(i).Gin = 0;
                ctltab(i).Rin = 0;
                ctltab(i).iHold = 0;
                ctltab(i).Vm0 = 0;
                ctltab(i).Rmp = 0;
                ctltab(i).entrydate = datestr(now, 0);
                ctltab(i).lastchange = datestr(now, 0);
                ctltab(i).subtract = 'no';
                ctltab(i).linkfile = '';
                ctltab(i).sub_frec = 1;
                ctltab(i).sub_lrec = 1;
                ctltab(i).sub_access = 0;
                ctltab(i).sub_comp = 0;
                ctltab(i).sub_igain = 1;

                %         setuserdata(ctltab); % update to the main list
                CONTROL = ctltab;
                watchoff;
            end
        end


    case 'open'
        if(exist(filename, 'file') == 2) % check for existence before loading
            watchon;
            [p f e] = fileparts(filename);
            QueMessage(sprintf('Loading database "%s"', f));
            load(filename);
            watchoff;
        else
            QueMessage(sprintf('File %s not found?', filename));
            err = 1;
            return;
        end

        CONTROL = control; % copy over for output
        %   setuserdata(control); % associate it for listcontrol.m to access
        nsf=size(CONTROL);
        QueMessage(sprintf('   Database "%s"[%d] loaded', f, nsf(2)));
        return

    case 'save'
        %   control = getuserdata;
        [p f e] = fileparts(filename);
        QueMessage(sprintf('Saving database %s', f));
        watchon;
        QueMessage(sprintf('Saving database %s - copy', f));
        control = CONTROL;
        QueMessage(sprintf('Saving database %s - save', f));
        save(filename, 'control');
        QueMessage(sprintf('  Database "%s" saved', filename));
        watchoff;
        return

    otherwise
        return
end
return


%------------------------------------------------------------------------
% directly read the file for IMPORTING
function [ctl, line, err] = readctlfile(filename)

err = 0;
fid=fopen(filename, 'r');
if(fid==0)
    err = 1;
    return
end
ctl=fgetl(fid);
i=1;
while feof(fid) == 0
    line(i).data = fgetl(fid);
    i = i+1;
end

%------------------------------------------------------------------------
function [k]=next()
global ctl_k
ctl_k = ctl_k + 1;
k = ctl_k;
return


%------------------------------------------------------------------------
function [token, remainder] = strtok(string, delimiters)
%STRTOK Find token in string. MODIFIED TO NOT SKIP TO FIRST
%   STRTOK(S) returns the first token in the string S delimited
%   by "white space".   Leading white space is ignored.
%
%   STRTOK(S,D) returns the first token delimited by one of the
%   characters in D.
%
%   [T,R] = STRTOK(...) also returns the remainder of the original
%   string.
%   If the token is not found in S then R is an empty string and T
%   is same as S.
%
%   See also ISSPACE.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.9 $  $Date: 1997/11/21 23:47:47 $

if nargin<1, error('Not enough input arguments.'); end

token = ' '; remainder = [];

len = length(string);
if len == 0
    return
end

if (nargin == 1)
    delimiters = [9:13, 32]; %[9:13 32]; % White space characters except spaces
end
i = 1;
start = i; % now get the next gizmo
while (~any(string(i) == delimiters))
    i = i + 1;
    if (i > len), break, end
end
finish = i - 1;
if(start <= finish)
    token = string(start:finish);
end

if (nargout == 2)
    remainder = string(finish + 2:length(string));
end




